package com.jvup.gameshow.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.EditText;

import androidx.annotation.NonNull;

import java.util.ArrayList;

/**
 * 复合事件编辑框
 */
@SuppressLint("AppCompatCustomView")
public class CompoundEventEditText extends EditText {

    private ArrayList<EventDrawable> eventDrawableList = new ArrayList<>();
    private PointF actionUpPoint = new PointF();
    private ColorDrawable placeHolderDrawable = new ColorDrawable(Color.TRANSPARENT);

    public EventDrawable addEventDrawable(@NonNull int rid, @NonNull String name){
        Drawable drawable = getResources().getDrawable(rid,null);
        return addEventDrawable(drawable,name);
    }

    public EventDrawable addEventDrawable(@NonNull String path, @NonNull String name){
        Drawable drawable = BitmapDrawable.createFromPath(path);
        return addEventDrawable(drawable,name);
    }

    public EventDrawable addEventDrawable(@NonNull Drawable drawable, @NonNull String name){
        return addEventDrawable(new EventDrawable(drawable,name));
    }

    public EventDrawable addEventDrawable(@NonNull EventDrawable eventDrawable){

        EventDrawable lastEventDrawable = null;
        if(!eventDrawableList.isEmpty())
            lastEventDrawable = eventDrawableList.get(eventDrawableList.size()-1);

        correctionOffset(eventDrawable,lastEventDrawable);
        eventDrawableList.add(eventDrawable);

        setPlaceHolderDrawable();

        return eventDrawable;
    }

    public void setPlaceHolderDrawable(){
        int width = 0;
        int height = 0;
        int index = 1;
        for (EventDrawable eDrawable : eventDrawableList){
            width += eDrawable.bounds.width() + index * 10;
            if(eDrawable.bounds.height() > height){
                height = (int) eDrawable.bounds.height();
            }
            index++;
        }
        placeHolderDrawable.setBounds(0, 0, width, height);
        setCompoundDrawables(null, null, placeHolderDrawable, null);
    }

    private void correctionOffset(EventDrawable eventDrawable,EventDrawable lastEventDrawable){
        int lastOffset = lastEventDrawable != null ? lastEventDrawable.width() : 0;
        eventDrawable.bounds.offsetTo(0,0);
        int offsetY = (getHeight() - (int)eventDrawable.bounds.height())/2;
        int offsetX = getWidth() - getPaddingRight();
        offsetX = offsetX - lastOffset;
        offsetX = offsetX - (int)eventDrawable.bounds.width();
        eventDrawable.bounds.offset(offsetX,offsetY);
    }

    public void setCompoundEventListener(String name, CompoundEventListener listener){
        for(EventDrawable drawable : eventDrawableList){
            if(name.equals(drawable.name))
                drawable.eventListener = listener;
        }
    }

    private void init(){

    }

    public CompoundEventEditText(Context context) {
        super(context);
        init();
    }

    public CompoundEventEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CompoundEventEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @Override
    public void layout(int l, int t, int r, int b) {
        super.layout(l, t, r, b);
        EventDrawable lastEventDrawable = null;
        for(EventDrawable eventDrawable : eventDrawableList){
            correctionOffset(eventDrawable,lastEventDrawable);
            lastEventDrawable = eventDrawable;
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        for(EventDrawable drawable : eventDrawableList){
            drawable.draw(canvas);
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        final int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_UP:{
                actionUpPoint.x = event.getX();
                actionUpPoint.y = event.getY();
                break;
            }
        }
        return super.onTouchEvent(event);
    }

    @Override
    public boolean performClick() {
        for(EventDrawable eventDrawable : eventDrawableList){
            boolean handled = eventDrawable.dispatchOnClick((int)actionUpPoint.x,(int)actionUpPoint.y);
            if(handled){
                return true;
            }
        }
        return super.performClick();
    }

    public interface CompoundEventListener{
        boolean onClick(CompoundEventEditText view, EventDrawable eventDrawable);
    }

    public class EventDrawable {

        public  final String name;
        public  final RectF bounds = new RectF();
        public  final int padding = 10;
        private boolean isVisible = true;
        private Drawable drawable;
        private CompoundEventListener eventListener;

        public EventDrawable(int rid, String name){
            Drawable drawable = getResources().getDrawable(rid,null);
            drawable.setBounds(new Rect(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight()));
            this.name = name;
            this.drawable = drawable;
            this.bounds.set(drawable.getBounds());
        }

        public EventDrawable(Drawable drawable, String name){
            drawable.setBounds(new Rect(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight()));
            this.name = name;
            this.drawable = drawable;
            this.bounds.set(drawable.getBounds());
        }

        public void setDrawableResId(int resId){
            Drawable drawable = getResources().getDrawable(resId,null);
            setDrawable(drawable);
        }

        public void setDrawable(Drawable drawable){
            drawable.setBounds(new Rect(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight()));
            this.drawable = drawable;
        }

        public void setVisible(boolean isVisible){
            this.isVisible=isVisible;
        }

        public int width(){
            RectF tmpBounds = new RectF(bounds);
            tmpBounds.inset(-padding,-padding);
            return (int) tmpBounds.width();
        }

        public boolean dispatchOnClick(int x,int y){
            if(eventListener == null)
                return false;
            RectF tmpBounds = new RectF(bounds);
            tmpBounds.inset(-padding/2,-padding);
            if(tmpBounds.contains(x,y)){
                return eventListener.onClick(CompoundEventEditText.this,this);
            }
            return false;
        }

        public void draw(@NonNull Canvas canvas) {
            if(!isVisible){
                return;
            }
            canvas.save();
            canvas.translate(bounds.left + getScrollX(),bounds.top);
            drawable.draw(canvas);
            canvas.restore();
        }

    }

}
